package com.augurit.gzcgwsp.rqqyjyxk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.excelimportexport.entity.YwRqQyjyxkRqssdjb;
import com.augurit.gzcgwsp.rqqyjyxk.dao.YwRqQyjyxkFinalRqssdjbDao;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwRqQyjyxkFinalRqssdjb;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkFinalRqssdjbService;
import com.augurit.gzcgwsp.rqqyjyxk.web.form.YwRqQyjyxkRqssdjbForm;
/**
 * 
 * <P>title:最终数据燃气设施登记service实现类</p>																								
 * <P>Description：</p>
 * <p>Copyright: Copyright (c) 2011 augurit</p>
 * <p>Company: 广州奥格智能科技</p>
 * History：
 * @author:zhangjun
 * @date Oct 15, 2011
 * @version 1.0
 */
@Service("rqssdjService")
@Transactional
public class YwRqQyjyxkFinalRqssdjbServiceImpl   implements IYwRqQyjyxkFinalRqssdjbService{
	@Autowired
	private YwRqQyjyxkFinalRqssdjbDao rqssdjDao;

    public List<YwRqQyjyxkFinalRqssdjb> getInfosByQyid(Long qyid) {
       String  sql=" from "+ YwRqQyjyxkFinalRqssdjb.class.getName()+" where qyid='"+qyid+"'";
        return rqssdjDao.find(sql);
    }

    public void save(YwRqQyjyxkFinalRqssdjb finalentity) {
        if(finalentity!=null &&finalentity.getId()!=null){
            //转换类的时候id不同了所以用这种方式更新
            Session session=rqssdjDao.getSession();
            session.flush();
            session.clear();
            session.saveOrUpdate(finalentity);
        }else{
            rqssdjDao.save(finalentity);
        }
        
    }

    /*
     * (non-Javadoc)
     * @see com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkFinalRqssdjbService#getPage(java.lang.String, org.springside.modules.orm.Page, java.lang.Long, java.lang.String)
     */
	public Page getPage(String acceptNo, Page page, Long qyid, String type) {
		 String  hql=" from "+ YwRqQyjyxkRqssdjb.class.getName()+" where acceptNo='"+acceptNo+"'";
		    if("2".equals(type)){
		    	hql=" from "+ YwRqQyjyxkFinalRqssdjb.class.getName()+" where qyid='"+qyid+"'";
		    	
		    }
		   
		    Page pages=rqssdjDao.findPage(page, hql, new HashMap());
		    YwRqQyjyxkRqssdjb entity=null;
		    YwRqQyjyxkFinalRqssdjb finalentity=null;
		    
		    if(pages.getResult().size()>0){
		    	if("2".equals(type)){
		    		finalentity=(YwRqQyjyxkFinalRqssdjb) pages.getResult().get(0);
		    		List<YwRqQyjyxkRqssdjbForm> list=this.chengFormByFinaleEntity(finalentity);
		    		pages.setTotalCount(10);
		    		pages.setResult(list);
		    	}else{
		    		entity=(YwRqQyjyxkRqssdjb) pages.getResult().get(0);
			    	List<YwRqQyjyxkRqssdjbForm> list=this.chengFormByEntity(entity);
			    	pages.setTotalCount(10);
			    	pages.setResult(list);
		    	}
		    }
		    return pages;
	}

	/**
	 * 转换为集合用于页面展示
	 * @param entity
	 * @return
	 */
	private List<YwRqQyjyxkRqssdjbForm> chengFormByEntity(YwRqQyjyxkRqssdjb entity){
		 List<YwRqQyjyxkRqssdjbForm> list=new ArrayList<YwRqQyjyxkRqssdjbForm>();
		 YwRqQyjyxkRqssdjbForm form =new YwRqQyjyxkRqssdjbForm(entity.getId(),"人工煤气生产厂",entity.getRgmqsccSl());
		 YwRqQyjyxkRqssdjbForm form1 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"储配站",entity.getCpzSl());
		 YwRqQyjyxkRqssdjbForm form2 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"门站",entity.getMzSl());
		 YwRqQyjyxkRqssdjbForm form3=new YwRqQyjyxkRqssdjbForm(entity.getId(),"气化站",entity.getQhzSl());
		 YwRqQyjyxkRqssdjbForm form4 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"混气站",entity.getHqzSl());
		 YwRqQyjyxkRqssdjbForm form5=new YwRqQyjyxkRqssdjbForm(entity.getId(),"加气站",entity.getJqzSl());
		 YwRqQyjyxkRqssdjbForm form6=new YwRqQyjyxkRqssdjbForm(entity.getId(),"灌装站",entity.getGzzSl());
		 YwRqQyjyxkRqssdjbForm form7=new YwRqQyjyxkRqssdjbForm(entity.getId(),"供应站",entity.getGyzSl());
		 YwRqQyjyxkRqssdjbForm form8=new YwRqQyjyxkRqssdjbForm(entity.getId(),"调压站",entity.getTyzSl());
		 YwRqQyjyxkRqssdjbForm form9=new YwRqQyjyxkRqssdjbForm(entity.getId(),"管道气营业点",entity.getGdqyydSl());
		 list.add(form);
		 list.add(form1);
		 list.add(form2);
		 list.add(form3);
		 list.add(form4);
		 list.add(form5);
		 list.add(form6);
		 list.add(form7);
		 list.add(form8);
		 list.add(form9);
		 return list;
	}
	

	private List<YwRqQyjyxkRqssdjbForm> chengFormByFinaleEntity(YwRqQyjyxkFinalRqssdjb entity){
		 List<YwRqQyjyxkRqssdjbForm> list=new ArrayList<YwRqQyjyxkRqssdjbForm>();
		 YwRqQyjyxkRqssdjbForm form =new YwRqQyjyxkRqssdjbForm(entity.getId(),"人工煤气生产厂",entity.getRgmqsccSl());
		 YwRqQyjyxkRqssdjbForm form1 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"储配站",entity.getCpzSl());
		 YwRqQyjyxkRqssdjbForm form2 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"门站",entity.getMzSl());
		 YwRqQyjyxkRqssdjbForm form3=new YwRqQyjyxkRqssdjbForm(entity.getId(),"气化站",entity.getQhzSl());
		 YwRqQyjyxkRqssdjbForm form4 =new YwRqQyjyxkRqssdjbForm(entity.getId(),"混气站",entity.getHqzSl());
		 YwRqQyjyxkRqssdjbForm form5=new YwRqQyjyxkRqssdjbForm(entity.getId(),"加气站",entity.getJqzSl());
		 YwRqQyjyxkRqssdjbForm form6=new YwRqQyjyxkRqssdjbForm(entity.getId(),"灌装站",entity.getGzzSl());
		 YwRqQyjyxkRqssdjbForm form7=new YwRqQyjyxkRqssdjbForm(entity.getId(),"供应站",entity.getGyzSl());
		 YwRqQyjyxkRqssdjbForm form8=new YwRqQyjyxkRqssdjbForm(entity.getId(),"调压站",entity.getTyzSl());
		 YwRqQyjyxkRqssdjbForm form9=new YwRqQyjyxkRqssdjbForm(entity.getId(),"管道气营业点",entity.getGdqyydSl());
		 list.add(form);
		 list.add(form1);
		 list.add(form2);
		 list.add(form3);
		 list.add(form4);
		 list.add(form5);
		 list.add(form6);
		 list.add(form7);
		 list.add(form8);
		 list.add(form9);
		 return list;
	}
	
}
